<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
            :confirmButtonText="isDetail ? '确认' : false"
        >
            <div v-if="!isDetail">
                <h3>模板名称：{{formData.templateName}}</h3>
                <h3>模版内容：{{formData.templateContent}}</h3>
                <h3>模版状态：
                    <el-radio-group v-model="formData.templateState" placeholder="请选择模板状态0禁用 1可用" >
                        <el-radio v-for="(item, index) in dictData.status" :key="index" :label="parseInt(item.value)" :disabled="!item.status">{{ item.name }}</el-radio>
                    </el-radio-group>
                </h3>
                <h3>模板显示状态：
                    <el-radio-group v-model="formData.state" placeholder="请选择状态0禁用 1可用" >
                        <el-radio v-for="(item, index) in dictData.status" :key="index" :label="parseInt(item.value)" :disabled="!item.status">{{ item.name }}</el-radio>
                    </el-radio-group>
                </h3>
                <h3>签署方：
                    <el-select class="flex-1" v-model="formData.contractType" placeholder="请选择签署方" >
                        <el-option
                            v-for="(item, index) in dictData.contract_type"
                            :key="index"
                            :label="item.name"
                            :value="parseInt(item.value)"
                            clearable
                            :disabled="!item.status"
                        />
                    </el-select>
                </h3>
                <h3>创建人：{{formData.founderName}}</h3>
                <h3>创建时间：{{formData.creationTimeStr}}</h3>
            </div>
            <div v-else>
                <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules" label-position="top">
                    <el-form-item label="模板名称" prop="templateName">
                        <el-input v-model="formData.templateName" placeholder="请输入模板名称" />
                    </el-form-item>
                    <el-form-item label="模板内容" prop="templateContent">
                        <el-input type="textarea" v-model="formData.templateContent"></el-input>
                    </el-form-item>
                    <el-form-item label="模板状态" prop="templateState">
                        <el-radio-group v-model="formData.templateState" placeholder="请选择模板状态0禁用 1可用">
                            <el-radio
                                v-for="(item, index) in dictData.status"
                                :key="index"
                                :label="parseInt(item.value)"
                                :disabled="!item.status">
                                {{ item.name }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="模板显示状态" prop="state">
                        <el-radio-group v-model="formData.state" placeholder="请选择状态0禁用 1可用">
                            <el-radio v-for="(item, index) in dictData.status" :key="index" :label="parseInt(item.value)" :disabled="!item.status">
                                {{ item.name }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="签署方" prop="contractType">
                        <el-select class="flex-1" v-model="formData.contractType" placeholder="请选择签署方">
                            <el-option
                                v-for="(item, index) in dictData.contract_type"
                                :key="index"
                                :label="item.name"
                                :value="parseInt(item.value)"
                                clearable
                                :disabled="!item.status"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="上传文件" prop="templateFile">
                        <el-upload
                            ref="uploadRef"
                            class="upload-demo"
                            :action="uploadUrl"
                            :auto-upload="true"
                            :headers="headers"
                            @success="handleUploadSuccess"
                            :show-file-list="true"
                        >
                            <el-button>上传文件</el-button>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="盖章位置" prop="sort">
                        <span style="width: 30px">X:</span><el-input-number v-model="formData.positionX" />
                        <span style="width: 30px; margin-left: 20px">Y:</span><el-input-number v-model="formData.positionY" />
                    </el-form-item>
                </el-form>
            </div>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  templateEdit, templateAdd, templateDetail } from '@/api/contract_template'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import config from "@/config";
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const uploadUrl = ref(config.baseUrl+'api/file/uploadFile');
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const isDetail = computed(() => {
    if (mode.value == 'add') {
        return true
    } else if (mode.value == 'edit') {
        return true
    } else {
        return false
    }
})
const popupTitle = computed(() => {
    if (mode.value == 'add') {
        return '新增调解书模板'
    } else if (mode.value == 'edit') {
        return '编辑调解书模板'
    } else {
        return '详情'
    }
})

const formData = reactive({
    templateNumber: '',
    contractType: '',
    templateName: '',
    templateContent: '',
    templateState: '',
    state: '',
    fileId: '',
    templateId: '',
    founderName: '',
    creationTimeStr: '',
    fileUploadUrl: '',
    docTemplateId: '',
    positionX: 0,
    positionY: 0,
})

const formRules = {

    contractType: [
        {
            required: true,
            message: '请选择签署方',
            trigger: ['blur']
        }
    ],
    templateName: [
        {
            required: true,
            message: '请输入模板名称',
            trigger: ['blur']
        }
    ],
    templateContent: [
        {
            required: true,
            message: '请输入模板内容',
            trigger: ['blur']
        }
    ],
    templateState: [
        {
            required: true,
            message: '请选择模板状态0禁用 1可用',
            trigger: ['blur']
        }
    ],
    founder: [
        {
            required: true,
            message: '请输入创建人',
            trigger: ['blur']
        }
    ],
    creationTime: [
        {
            required: true,
            message: '请选择创建时间',
            trigger: ['blur']
        }
    ],
    modifier: [
        {
            required: true,
            message: '请输入修改人',
            trigger: ['blur']
        }
    ],
    modifierTime: [
        {
            required: true,
            message: '请选择修改时间',
            trigger: ['blur']
        }
    ],
    state: [
        {
            required: true,
            message: '请选择状态0禁用 1可用',
            trigger: ['blur']
        }
    ],
    formworkDrawing: [
        {
            required: true,
            message: '请输入模板图',
            trigger: ['blur']
        }
    ],
    fileId: [
        {
            required: true,
            message: '请输入待签署文件id',
            trigger: ['blur']
        }
    ],
    fileUploadUrl: [
        {
            required: true,
            message: '请输入文件下载地址',
            trigger: ['blur']
        }
    ],
    positionX: [
        {
            required: true,
            message: '请输入文件盖章x坐标',
            trigger: ['blur']
        }
    ],
    positionY: [
        {
            required: true,
            message: '请输入文件盖章y坐标',
            trigger: ['blur']
        }
    ],
    templateId: [
        {
            required: true,
            message: '请输入制作模板控件的编号',
            trigger: ['blur']
        }
    ],
    isMediationDocument: [
        {
            required: true,
            message: '请输入是否调解书',
            trigger: ['blur']
        }
    ],
    // templateFile: [
    //     {
    //         required: true,
    //         message: '请上传合同模板',
    //         trigger: ['blur']
    //     }
    // ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await templateEdit(data) : await templateAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const handleUploadSuccess = (response: any) => {
    // 处理上传成功的回调
    formData.fileId = response.data.fileId;
    formData.fileUploadUrl = response.data.fileUploadUrl;
    formData.templateId = response.data.docTemplateId;
    feedback.msgSuccess('文件上传成功')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await templateDetail({
        templateNumber: row.templateNumber
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
